<?php
/**
 * Yucy调度器主类
 * 
 * @author Yucy<yucy1985@gmail.com>
 *
 */
class YucyCore
{
	/**
	 * 
	 * Yucy对象
	 * @var object
	 */
	private static $_instance;
	
	/**
	 * 
	 * 调度器对象
	 * @var object
	 */
	private $_dispatcher;
	
	/**
	 * 
	 * 调度器名称
	 * @var string
	 */
	private $_dispatcherName = null;
	
	
	/**
	 * 
	 * 助手对象
	 * @var object
	 */
	private $_helper;
	
	
	/**
	 * 
	 * 访客对象
	 * @var object
	 */
	private $_visitor;
	
	
	/**
	 * 
	 * 资源请求对象
	 * @var object
	 */
	private $_request;
	
	
	/**
	 * 
	 * 资源响应对象
	 * @var object
	 */
	private $_response;
	
	
	/**
	 * 
	 * 系统日志对象
	 * @var object
	 */
	private $_logger;
	

	/**
	 * 
	 * 获得类的实例
	 */
	public static function getInstance()
	{
		if (!self::$_instance) self::$_instance = new self();
		
		return self::$_instance;
	}
	
	
	public static function getApp()
	{
		return self::getInstance();
	}
	
	
	/**
	 * 
	 * 实例化类
	 */
	private function __construct()
	{
		// 设置系统时区
		if (YucyConfig::get('timezone') && function_exists('date_default_timezone_set'))
			date_default_timezone_set(YucyConfig::get('timezone'));
			
		if (YucyConfig::get('is_debug')) return ;
		
		// 设置错误处理
		set_error_handler('YucyLogger::errorHandler');
		
		// 设置异常捕捉
		set_exception_handler('YucyLogger::exceptionHandler');
	}
	
	
	/**
     * 
     * 运行应用实例
     * @return void
     */
    public function run()
	{
		// 运行调度器
		$this->getDispatcher()->run();
        
        return ;
    }
    
    
    /**
     * 
     * 设置调度器名称
     * @param string $dispatcherName
     */
    public function setDispatcherName($dispatcherName)
    {
    	$this->_dispatcherName = (string)$dispatcherName;
    	
    	return $this;
    }
    
    
    /**
     * 
     * 获取调度器名称
     * @return string
     */
    public function getDispatcherName()
    {
    	if ($this->_dispatcherName !== null) return $this->_dispatcherName;
    	
    	$this->setDispatcherName(YucyConfig::get('dispatcher_name'));
    	
    	return $this->_dispatcherName;
    }
    
    
    /**
     * 
     * 获取调度器
     * @return object
     */
    public function getDispatcher()
    {
    	if ($this->_dispatcher) return $this->_dispatcher;
    	
    	$dispatcher_name = $this->getDispatcherName();
    	if (!$dispatcher_name)
    	{
    		$this->_dispatcher = new YucyDispatcher;
    		return $this->_dispatcher;
    	}
    	
    	$class_name = ucfirst($dispatcher_name) . 'Dispatcher';
    	YucyAutoloader::load($class_name);
    	
    	$this->_dispatcher = new $class_name;
    	if ($this->_dispatcher instanceof YucyDispatcher) return $this->_dispatcher;
    	
    	throw new YucyException("您指定的调度器对象没有继承{YucyDispathcer}类", 1);
    }
    
    
    /**
     * 
     * 重置调度器
     */
    public function resetDispatcher()
    {
    	$this->_dispatcher = null;
    	
    	return $this->getDispatcher();
    }
    
    
    /**
     * 
     * 获得助手对象
     * @return YucyHelper
     */
    public function getHelper()
    {
    	if ($this->_helper) return $this->_helper;
    	
    	$this->_helper = new YucyHelper();
    	
    	return $this->_helper;
    }
    
    
    /**
     * 
     * 获得访客对象
     * @return YucyVisitor
     */
    public function getVisitor()
    {
    	if ($this->_visitor) return $this->_visitor;
    	
    	$this->_visitor = new YucyVisitor();
    	
    	return $this->_visitor;
    }
    
    
    /**
     * 
     * 设置访客对象
     * @param YucyVisitor $visitor
     * @return object
     */
    public function setVisitor($visitor)
    {
    	if ($visitor instanceof YucyVisitor)
    	{
    		$this->_visitor = $visitor;
    		return $this;
    	}
    	
    	throw new YucyException("您指定的访客对象没有继承{YucyVisitor}类", 2);
    }
    
    
    /**
     * 
     * 获得请求对象
     * @return object
     */
    public function getRequest()
    {
    	if ($this->_request) return $this->_request;
    	
    	$this->_request = $this->getHelper()->getRequest();
    	
    	return $this->_request;
    }
    
    
     /**
     * 
     * 获得响应对象
     * @return object
     */
    public function getResponse()
    {
    	if ($this->_response) return $this->_response;
    	
    	$this->_response = $this->getHelper()->getResponse();
    	
    	return $this->_response;
    }
    
    
    /**
     * 
     * 获取系统日志对象
     */
    public function getLogger()
    {
    	if ($this->_logger) return $this->_logger;
    	
    	$this->_logger = new YucyLogger();
    	
    	return $this->_logger;
    }
}
?>